<?php
/**
 * OS CRM SYSTEM - An open source application development framework for PHP 4.3.2 or newer
 *
 * CLASS - JOURNALS - OS Admin
 * 
 * @name      OS Admin
 * @package		OS CRM
 * @category  Class
 * @author		CRM SYSTEM Dev Team
 * @copyright	Copyright (c) 2010 - 2012, CRM SYSTEM
 * @since     Version 1.0
 * @filesource
 * @access    public 
 */

// ------------------------------------------------------------------------

class os_admin extends Controller {
  
  /*common class varibale*/
  var $c 	= 'os_admin';//current controler
	var $p_c = 'volumes';//parent controller  
  var $cur_c = 'volumes/os_admin';//current path controller
  
  //set db - listing data
	var $e_list_limit 	= '10';
	var $e_list_offset 	= '0';
	var $e_list_order   = 'asc';
	var $e_list_order_by= 'os_volumes_id'; 
  
  /*message strings*/
  var $valid_success 	= '';
  var $valid_errors 	= '';
  var $valid_notice 	= '';
  
  /*search page key*/
  var $ses_search_key= 'vol_admin_search';
  
  /**
   * os_admin  - constructor of the class
   * 
   * @access public
   * @param  NA
   * @return void
   */
	function os_admin()
	{
		parent::Controller();	
    
    if(!__admin_loggedin()){
      redirect(OS_CRM_SITEADMIN);
    }
    
    $this->lang->load('common', ADMIN_LANGUAGE);
    $this->load->model('DB_Model', 'db_model');
	}
	
  /** 
	 * Index
	 *
	 * @desc NA
	 * @access public/private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function index()
	{
    $this->list_all();            
	}
  
  /** 
	 * SetJ - journal ID set
	 *
	 * @desc NA
	 * @access public/private
	 * @param  int journal id
	 * @return void
	 */
	function setj($id = 0){
    $id = (int) $id;
    if(!empty($id)){
      $this->_set_jid($id);
      redirect($this->cur_c.'/list_all');
    }else{
      redirect(OS_CRM_SITEADMIN);
    }
	}
  
  /** 
	 * Set JID - journal ID set
	 *
	 * @desc NA
	 * @access public/private
	 * @param  int journal id
	 * @return void
	 */
	function _set_jid($id = 0){
    $this->session->set_userdata('jid',$id);
	}
  
  /** 
	 * Get JID - journal ID set
	 *
	 * @desc NA
	 * @access public/private
	 * @param  NA
	 * @return int journal id
	 */
	function _get_jid(){
    $id = $this->session->userdata('jid');
    return $id;
	}
 
	/** 
	 * List All
	 *
	 * @descp List all the pages in the system
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function list_all()
	{
		$data = array();
		$data['cont_title'] = 'Volume Management System';
		
    /*set pagination items*/        
    $this->load->library('Digg_Pagination');
    $this->load->plugin('pagination');      
		
    $this->db_model->e_jid = $this->_get_jid();
    
    /*get count and set pagination*/
		$result_cnt_set = $this->db_model->bk_get_list_cnt();    
    $per_page_seg = 4;
    $cur_page_seg = 5;
    $per_page_val = get_per_page($this->e_list_limit, $per_page_seg);    
    $total_rows = $result_cnt_set['list']['cnt'];
    $this->db_model->e_list_offset = get_offset($cur_page_seg, $per_page_val);    
    $pbase_url = site_url($this->cur_c .'/list_all/');
    
    $pagination_link = init_paginate($cur_page_seg, $total_rows, $per_page_val, $per_page_seg, $pbase_url);
    $data['pagination_link']   = $pagination_link;
    
		//get all pages
		$result_set       = $this->db_model->bk_get_list();
		$data['list'] 		= $result_set['list'];
		$data['num_rows'] = $result_cnt_set['list']['cnt'];/*total rows*/
		
    $data['frm_name']   = 'frmvolumelist';
    $data['frm_action'] = site_url( $this->c );
    
		//set crontroller info
		$data['c'] 		= $this->c;
		$data['p_c'] 	= $this->p_c;
    $data['cur_c'] 	= $this->cur_c;
    
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->session->flashdata('valid_errors');
    $data['msg_info']['valid_notice'] = $this->session->flashdata('valid_notice');
    $data['msg_info']['valid_success'] = $this->session->flashdata('valid_success');
    
		__admin_display('listing',$data);
	}
	  
	/** 
	 * Add - page
	 *
	 * @descp interface to add the page details
	 * @access public
	 * @param  na
	 * @return void
	 */
	function add()
	{
		$id = 0;
		$token = random_string();
    $post_arr = array();
    $post_arr['os_journal_id']        = $this->_get_jid();
    $post_arr['os_volume_id']         = $this->input->post('os_volume_id');
    $post_arr['os_volume_year']       = $this->input->post('os_volume_year');
    $post_arr['os_volume_no']         = $this->input->post('os_volume_no');
    $post_arr['os_volume_issue_no']   = $this->input->post('os_volume_issue_no');
    $post_arr['os_volume_issue_dt_range'] = $this->input->post('os_volume_issue_dt_range');
    $post_arr['os_volume_abtract']    = $this->input->post('os_volume_abtract');
    $post_arr['os_volume_content']    = $this->input->post('os_volume_content');
    $post_arr['os_volume_price']      = $this->input->post('os_volume_price');
    $post_arr['os_volume_pages']      = $this->input->post('os_volume_pages');
    $post_arr['os_volume_created']     = $this->input->post('os_volume_created'); 
    $post_arr['os_volume_status']     = $this->input->post('os_volume_status');
    
    
		$data = array();
		$data['cont_title'] 	= 'Add new record';		
    $data['post_arr']     = $post_arr;
		
		/*set crontroller info*/
		$data['c'] 	= $this->c;
		$data['p_c'] 	= $this->p_c;
		$data['cur_c'] 	= $this->cur_c;
        
    /*set form actions*/
    $data['frm_name']   = 'frmvolumeadd';
    $data['frm_action'] = site_url( $this->cur_c .'/insert');
    $data['frm_token']  = $token;
    $data['frm_mode']   = 'add';
    $data['frm_id']      = $id;
        
    $this->session->set_userdata('frm_token', $token);
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->valid_errors;
    $data['msg_info']['valid_notice'] = $this->valid_notice;
    $data['msg_info']['valid_success'] = $this->valid_success;
    
		__admin_display('add',$data);
	}
	
	/** 
	 * Edit - page
	 *
	 * @descp interface to edit the page details
	 * @access public
	 * @param  integer - page id	 
	 * @return void
	 */
	function edit($id = 0)
	{
		$id = (int) $id;
		
		if(!empty($id))
		{
			$token = random_string();
      $post_arr = array();
      
      if (!$_POST) {        
        if ($post_arr = $this->db_model->bk_get_data($id)) {                                        
        } else {
          redirect($this->cur_c);
        }
      } else {
        $post_arr = __set_post_data();
      }
      
      $post_arr['os_journal_id']        = $this->_get_jid();
      
      $data = array();
      $data['cont_title'] 	= 'Edit Record ID: '.$id;
      $data['post_arr']     = $post_arr;

      /*set crontroller info*/
      $data['c'] 	= $this->c;
      $data['p_c'] 	= $this->p_c;      
      $data['cur_c'] 	= $this->cur_c;
    
      /*set form actions*/
      $data['frm_name']   = 'frmvolumeedit';
      $data['frm_action'] = site_url( $this->cur_c .'/update');
      $data['frm_token']  = $token;
      $data['frm_mode']   = 'edit';
      $data['frm_id']      = $id;
      
      $this->session->set_userdata('frm_token', $token);
      /*set message strings*/
      $data['msg_info']['valid_errors'] = $this->valid_errors;
      $data['msg_info']['valid_notice'] = $this->valid_notice;
      $data['msg_info']['valid_success'] = $this->valid_success;
      
      __admin_display('add',$data);
		}
		else
		{
			redirect($this->cur_c);
		}
	}
	
	 
	 
  
  /** 
	 * Insert
	 *
	 * @desc this will validate and insert a new record
	 * @access public
	 * @param  NA	 
	 * @return mixed
	 */
	function insert()
	{
		$this->load->library('form_validation');
		$login_status     = false;
		$data             = array();
    $frm_token        = $this->input->post('frm_token');
		$sess_frm_token   = $this->session->userdata('frm_token');
    $this->session->unset_userdata('frm_token');
		$errors = $this->_val_form();     
		if(!empty($errors)){
			$this->valid_errors =  $errors;
      $this->add();
    }else if($frm_token != $sess_frm_token){      
			$this->valid_errors =  '<p>&raquo; Sorry form cannot be posted twice, please re-enter the details</p>';
      $this->add();
    }else{
      $this->db_model->e_jid = $this->_get_jid();
      $last_id = $this->db_model->bk_insert();
      //__debug($last_id);     
      
      $msg = $this->lang->line('db_insert_success');
      $this->session->set_flashdata('valid_success', sprintf($msg, $last_id));
      //__debug($this->session->flashdata('valid_success'));
      redirect($this->cur_c . '/list_all');
    }
	}
  
  
  
  /** 
	 * Update
	 *
	 * @desc this will validate and update a record
	 * @access public
	 * @param  NA	 
	 * @return mixed
	 */
	function update()
	{   
		$this->load->library('form_validation');
		$login_status     = false;
		$data             = array();
    $frm_token        = $this->input->post('frm_token');
    $frm_id           = (int) $this->input->post('frm_id');
    
		$sess_frm_token   = $this->session->userdata('frm_token');
    $this->session->unset_userdata('frm_token');
    
		$errors = $this->_val_form();    
    if(empty($frm_id)){
      redirect($this->c);
    }else if(!empty($errors)){      
			$this->valid_errors =  $errors;
      $this->edit($frm_id);
    }else if($frm_token != $sess_frm_token){      
			$this->valid_errors =  '<p>&raquo; Sorry form cannot be posted twice, please re-enter the details</p>';
      $this->edit($frm_id);
    }else{
      $this->db_model->e_jid = $this->_get_jid();
      $this->db_model->bk_update($frm_id);
      $last_id = $frm_id;
      //__debug($last_id);
      
      $msg = $this->lang->line('db_upd_success');
      $this->session->set_flashdata('valid_success', sprintf($msg, $frm_id));
      //__debug($this->session->flashdata('valid_success'));
      redirect($this->cur_c . '/list_all');
    }
	}
 

  
  /** 
	 * Validation Form
	 *
	 * @descp NA
	 * @access private
	 * @param  NA	 
	 * @return array
	 */
	function _val_form()
	{
		$config =  array();    
    $config[] = array('field' => 'os_volume_no','label' => 'volume no.','rules' => 'required|xss_clean');
    $config[] = array('field' => 'os_volume_year','label' => 'volume year','rules' => 'required|xss_clean');
    $config[] = array('field' => 'os_volume_issue_no','label' => 'issue no.','rules' => 'required|xss_clean');
		$config[] = array('field' => 'os_volume_issue_dt_range','label' => 'volume date','rules' => 'trim|required|min_length[2]|xss_clean');
		$config[] = array('field' => 'os_volume_price','label' => 'price','rules' => 'trim|min_length[2]|xss_clean');
		$config[] = array('field' => 'os_volume_pages','label' => 'issn','rules' => 'trim|min_length[2]|xss_clean');
    $config[] = array('field' => 'os_volume_abtract','label' => 'abstract','rules' => 'trim|min_length[20]|xss_clean');
    $config[] = array('field' => 'os_volume_content','label' => 'content','rules' => 'trim|min_length[20]|xss_clean');
		$this->form_validation->set_rules($config); 
		$this->form_validation->set_error_delimiters('<p>&raquo; ', '</p>');
		if ($this->form_validation->run() == false)
		{
			return validation_errors(); 
		}
	}
  
  
  /****************** search related function :start **********************/
  /** 
	 * search
	 *
	 * @descp List all the pages in the system
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function search()
	{
    if($_POST){
      $this->_set_search();
    }else{
      $this->_clear_search();
    }
    $this->searchlist();
  }
  
  /** 
	 * List All
	 *
	 * @descp List all the pages in the system
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function searchlist()
	{
		$data = array();
		$data['cont_title'] = 'Volume Management System';
		
    /*set pagination items*/        
    $this->load->library('Digg_Pagination');
    $this->load->plugin('pagination');      
		
    $this->db_model->e_jid = $this->_get_jid();
    
    $search_data = $this->_get_search();
    /*get count and set pagination*/
		$result_cnt_set = $this->db_model->bk_get_srch_list($search_data['qry'],TRUE);    
    $per_page_seg = 4;
    $cur_page_seg = 5;
    $per_page_val = get_per_page($this->e_list_limit, $per_page_seg);    
    $total_rows = $result_cnt_set['list']['cnt'];
    $this->db_model->e_list_offset = get_offset($cur_page_seg, $per_page_val);    
    $pbase_url = site_url($this->cur_c .'/searchlist/');
    
    $pagination_link = init_paginate($cur_page_seg, $total_rows, $per_page_val, $per_page_seg, $pbase_url);
    $data['pagination_link']   = $pagination_link;
    
		//get all pages
		$result_set       = $this->db_model->bk_get_srch_list($search_data['qry']);
		$data['list'] 		= $result_set['list'];
		$data['num_rows'] = $result_cnt_set['list']['cnt'];/*total rows*/
		
    $data['frm_name']   = 'frmvolumelist';
    $data['frm_action'] = site_url( $this->c );
    
		//set crontroller info
		$data['c'] 		= $this->c;
		$data['p_c'] 	= $this->p_c;
    $data['cur_c'] 	= $this->cur_c;
    
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->session->flashdata('valid_errors');
    $data['msg_info']['valid_notice'] = $this->session->flashdata('valid_notice');
    $data['msg_info']['valid_success'] = $this->session->flashdata('valid_success');
    
		__admin_display('listing',$data);
	}
  
  /** 
	 * Set Search
	 *
	 * @desc NA
	 * @access private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function _set_search()
	{
		$array_list = array();
    $array_list['srch_all_flds'] = $this->input->post("srch_all_flds");    
    $this->session->set_userdata($this->ses_search_key,$array_list);
	}
  
  /** 
	 * Get Search
	 *
	 * @desc NA
	 * @access private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function _get_search()
	{
    $return_qry  = array();
		$array_list = array();
    $search_str = '';
    $array_list = $this->session->userdata($this->ses_search_key);
    if(count($array_list) > 0){
      $search_str .= "(
                      os_volume_year ".filter_data($array_list['srch_all_flds'],'search_like')." 
                      OR
                      os_volume_issue_dt_range ".filter_data($array_list['srch_all_flds'],'search_like')." 
                      OR
                      os_volume_content ".filter_data($array_list['srch_all_flds'],'search_like')."                         
                      ) ";
    }
    $return_qry['list'] = $array_list;
    $return_qry['qry']  = $search_str;
    return $return_qry;
	}
  
  /** 
	 * Clear Seacrh
	 *
	 * @desc NA
	 * @access private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function _clear_search()
	{
		$array_list = array();
    $array_list['srch_all_flds'] = '';
    $this->session->set_userdata($this->ses_search_key,$array_list);
    $this->session->unset_userdata($this->ses_search_key);
	}
  
  /****************** search related function :end   **********************/
  
  
  /****************** change status function :start **********************/
  
  /** 
	 * Change Status
	 *
	 * @desc NA
	 * @access private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function change_status($status= 0)
	{
    $id = $this->input->post('frm_id'); 
    $frm_cur = $this->input->post('frm_cur'); 
    $id = (int) $id;
    $status = (int) $status;
		$result_cnt_set = $this->db_model->bk_update_status($id,$status);    
    $msg = $this->lang->line('db_upd_success');
    $this->session->set_flashdata('valid_success', sprintf($msg, $id));
    //__debug($this->session->flashdata('valid_success'));
    redirect($frm_cur);
	}
  /****************** change status function :end   **********************/
}

/* End of file login.php */
/* Location: ./system/application/modules/login/controllers/login.php */